﻿using System;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Studio
{
	// Token: 0x020004BD RID: 1213
	public class GuideMove : GuideBase, IInitializePotentialDragHandler, IPointerDownHandler, IPointerUpHandler, IEventSystemHandler
	{
		// Token: 0x17000373 RID: 883
		// (get) Token: 0x06001ECA RID: 7882 RVA: 0x000C0DE0 File Offset: 0x000BEFE0
		private Camera camera
		{
			get
			{
				if (this.m_Camera == null)
				{
					this.m_Camera = Camera.main;
				}
				return this.m_Camera;
			}
		}

		// Token: 0x06001ECB RID: 7883 RVA: 0x000C0E04 File Offset: 0x000BF004
		public void OnPointerDown(PointerEventData eventData)
		{
		}

		// Token: 0x06001ECC RID: 7884 RVA: 0x000C0E08 File Offset: 0x000BF008
		public void OnInitializePotentialDrag(PointerEventData eventData)
		{
			this.oldPos = eventData.pressPosition;
			GuideObject[] selectObjects = Singleton<GuideObjectManager>.Instance.selectObjects;
			this.dicTarget = selectObjects.ToDictionary((GuideObject v) => v.changeAmount, (GuideObject v) => v.transformTarget);
			Dictionary<int, ChangeAmount> selectObjectDictionary = Singleton<GuideObjectManager>.Instance.selectObjectDictionary;
			this.dicOld = selectObjectDictionary.ToDictionary((KeyValuePair<int, ChangeAmount> v) => v.Key, (KeyValuePair<int, ChangeAmount> v) => v.Value.pos);
			this.isSnap = true;
		}

		// Token: 0x06001ECD RID: 7885 RVA: 0x000C0ECC File Offset: 0x000BF0CC
		public override void OnDrag(PointerEventData eventData)
		{
			base.OnDrag(eventData);
			bool flag = false;
			Vector3 b = (this.axis != GuideMove.MoveAxis.XYZ) ? this.AxisMove(eventData.delta, ref flag) : (this.WorldPos(eventData.position) - this.WorldPos(this.oldPos));
			if (this.axis == GuideMove.MoveAxis.XYZ)
			{
				foreach (KeyValuePair<ChangeAmount, Transform> keyValuePair in this.dicTarget)
				{
					Vector3 vector = keyValuePair.Value.position;
					vector += b;
					keyValuePair.Value.position = vector;
					keyValuePair.Key.pos = keyValuePair.Value.localPosition;
				}
			}
			else
			{
				foreach (KeyValuePair<ChangeAmount, Transform> keyValuePair2 in this.dicTarget)
				{
					Vector3 vector2 = keyValuePair2.Key.pos;
					vector2 += b;
					keyValuePair2.Key.pos = ((!flag) ? vector2 : this.Parse(vector2));
				}
			}
			this.oldPos = eventData.position;
		}

		// Token: 0x06001ECE RID: 7886 RVA: 0x000C1044 File Offset: 0x000BF244
		public void OnPointerUp(PointerEventData eventData)
		{
		}

		// Token: 0x06001ECF RID: 7887 RVA: 0x000C1048 File Offset: 0x000BF248
		public override void OnEndDrag(PointerEventData eventData)
		{
			base.OnEndDrag(eventData);
			GuideCommand.EqualsInfo[] changeAmountInfo = (from v in Singleton<GuideObjectManager>.Instance.selectObjectDictionary
			select new GuideCommand.EqualsInfo
			{
				dicKey = v.Key,
				oldValue = this.dicOld[v.Key],
				newValue = v.Value.pos
			}).ToArray<GuideCommand.EqualsInfo>();
			Singleton<UndoRedoManager>.Instance.Push(new GuideCommand.MoveEqualsCommand(changeAmountInfo));
		}

		// Token: 0x06001ED0 RID: 7888 RVA: 0x000C1090 File Offset: 0x000BF290
		private Vector3 WorldPos(Vector2 _screenPos)
		{
			Plane plane = new Plane(this.camera.transform.forward * -1f, base.transform.position);
			Ray ray = RectTransformUtility.ScreenPointToRay(this.camera, _screenPos);
			float distance = 0f;
			return (!plane.Raycast(ray, out distance)) ? base.transform.position : ray.GetPoint(distance);
		}

		// Token: 0x06001ED1 RID: 7889 RVA: 0x000C1104 File Offset: 0x000BF304
		private Vector3 AxisPos(Vector2 _screenPos)
		{
			Vector3 position = base.transform.position;
			Plane plane = new Plane(base.transform.forward, position);
			if (!plane.GetSide(this.camera.transform.position))
			{
				plane = new Plane(base.transform.forward * -1f, position);
			}
			Vector3 up = base.transform.up;
			Ray ray = RectTransformUtility.ScreenPointToRay(this.camera, _screenPos);
			float distance = 0f;
			return (!plane.Raycast(ray, out distance)) ? Vector3.Project(position, up) : Vector3.Project(ray.GetPoint(distance), up);
		}

		// Token: 0x06001ED2 RID: 7890 RVA: 0x000C11B4 File Offset: 0x000BF3B4
		private Vector3 AxisMove(Vector2 _delta, ref bool _snap)
		{
			Vector3 vector = this.camera.transform.TransformVector(_delta.x * 0.005f, _delta.y * 0.005f, 0f);
			if (Input.GetKey(KeyCode.V) && vector[(int)this.axis] != 0f)
			{
				float num = (vector[(int)this.axis] >= 0f) ? 1f : -1f;
				vector = Vector3.zero;
				if (this.isSnap)
				{
					int num2 = Mathf.Clamp(Studio.optionSystem.snap, 0, 2);
					float[] array = new float[]
					{
						0.01f,
						0.1f,
						1f
					};
					vector[(int)this.axis] = array[num2] * num;
					this.isSnap = false;
					Observable.Timer(TimeSpan.FromMilliseconds(50.0)).Subscribe(delegate(long _)
					{
						this.isSnap = true;
					}).AddTo(this);
					_snap = true;
				}
			}
			else
			{
				vector *= Studio.optionSystem.manipuleteSpeed;
			}
			GuideMove.MoveAxis moveAxis = this.axis;
			if (moveAxis != GuideMove.MoveAxis.X)
			{
				if (moveAxis != GuideMove.MoveAxis.Y)
				{
					if (moveAxis == GuideMove.MoveAxis.Z)
					{
						vector = Vector3.Scale(Vector3.forward, vector);
					}
				}
				else
				{
					vector = Vector3.Scale(Vector3.up, vector);
				}
			}
			else
			{
				vector = Vector3.Scale(Vector3.right, vector);
			}
			return vector;
		}

		// Token: 0x06001ED3 RID: 7891 RVA: 0x000C1328 File Offset: 0x000BF528
		private Vector3 Parse(Vector3 _src)
		{
			string format = string.Format("F{0}", 2 - Studio.optionSystem.snap);
			_src[(int)this.axis] = float.Parse(_src[(int)this.axis].ToString(format));
			return _src;
		}

		// Token: 0x040020C2 RID: 8386
		public GuideMove.MoveAxis axis;

		// Token: 0x040020C3 RID: 8387
		private Vector2 oldPos = Vector2.zero;

		// Token: 0x040020C4 RID: 8388
		private Camera m_Camera;

		// Token: 0x040020C5 RID: 8389
		private Dictionary<int, Vector3> dicOld;

		// Token: 0x040020C6 RID: 8390
		private Dictionary<ChangeAmount, Transform> dicTarget;

		// Token: 0x040020C7 RID: 8391
		private bool isSnap = true;

		// Token: 0x020004BE RID: 1214
		public enum MoveAxis
		{
			// Token: 0x040020CD RID: 8397
			X,
			// Token: 0x040020CE RID: 8398
			Y,
			// Token: 0x040020CF RID: 8399
			Z,
			// Token: 0x040020D0 RID: 8400
			XYZ
		}
	}
}
